package org.example.friend.service.Impl;

import org.example.friend.entity.Friend_R;
import org.example.friend.entity.friend;
import org.example.friend.repository.FriendRepository;
import org.example.friend.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FriendServiceImpl implements FriendService {
    private final FriendRepository friendMapper;

    public FriendServiceImpl(FriendRepository friendMapper) {
        this.friendMapper = friendMapper;
    }

    @Override
    @Cacheable(value = "FriendCache", sync = true)
    public List<friend> getFriendList(String Email){
        List<friend> FriendList= friendMapper.getFriendList(Email);
        return FriendList;
    }
    @Override
    @CacheEvict(value = "FriendCache", allEntries = true, beforeInvocation = true)
    public void deleteFriend(String email,String friendEmail){
        Map<String, String> map=new HashMap<>();
        map.put("friendEmail",email);
        map.put("anotherEmail",friendEmail);
        friendMapper.deleteFriend(map);
    }
    @Override
    @CacheEvict(value = "FriendCache", allEntries = true, beforeInvocation = true)
    public void addFriend(String email,String friendEmail){
        Map<String, String> map=new HashMap<>();
        map.put("friendEmail",email);
        map.put("anotherEmail",friendEmail);
        friendMapper.addFriend(map);
    }
    //右边的加好友搜索
    @Override
    public friend findUserByEmail(String email,String userEmail){
        Map<String, String> map=new HashMap<>();
        map.put("friendEmail",email);
        map.put("anotherEmail",userEmail);
        return friendMapper.findUserByEmail(map);
    }
    //左边的找好友搜索
    @Override
    public friend findFriendByEmail(String email,String userEmail){
        Map<String, String> map=new HashMap<>();
        map.put("friendEmail",email);
        map.put("anotherEmail",userEmail);
        return friendMapper.findFriendByEmail(map);
    }
    @Override
    public List<Friend_R> getMyFriendRequest(String UserEmail){
        return friendMapper.getMyFriendRequest(UserEmail);
    }
    @Override
    public List<Friend_R> getMyFriendResponse(String UserEmail){
        return friendMapper.getMyFriendResponse(UserEmail);
    }
    @Override
    public void addFriendRequest(Friend_R friend_r){
        friendMapper.addFriendRequest(friend_r);
    }
    @Override
    public void deleteFriendRequest(Map map){
        friendMapper.deleteFriendRequest(map);
    }
}
